#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>
using namespace std;
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_Types.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_SDPProtocol/SDPProtocol_Define.h"
#include "../../../NetEngine_Linux/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_SDPProtocol/SDPProtocol_Error.h"

//g++ -std=gnu++11 -Wall -g Test_SDPProtocol.cpp -o Test_SDPProtocol -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib -L ../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_RfcComponents -lNetEngine_BaseLib -lRfcComponents_SDPProtocol -Wl,-rpath=../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_BaseLib:../../../NetEngine_Linux/NetEngine_Release/NetEngine_Release/NetEngine_RfcComponents,--disable-new-dtags

void CreateSDP(TCHAR *ptszMsgBuffer, int *pInt_Len)
{
    XNETHANDLE xhToken = 0;
    RfcComponents_SDPPacket_Create(&xhToken);

    RfcComponents_SDPPacket_Owner(xhToken, _T("qyt"), 123456789, _T("192.168.1.101"));
    RfcComponents_SDPPacket_Session(xhToken, _T("video.h264"));
    RfcComponents_SDPPacket_KeepTime(xhToken);
    //配置视频属性
    RfcComponents_SDPPacket_AddMedia(xhToken, _T("video"), 96);
    RFCCOMPONENTS_SDPPROTOCOL_MEDIAVIDEO st_SDPMediaVideo;
    memset(&st_SDPMediaVideo, '\0', sizeof(RFCCOMPONENTS_SDPPROTOCOL_MEDIAVIDEO));

    st_SDPMediaVideo.nTrackID = 1;
    st_SDPMediaVideo.st_RTPMap.nCodecNumber = 96;
    st_SDPMediaVideo.st_RTPMap.nSampleRate = 90000;
    _tcscpy(st_SDPMediaVideo.st_RTPMap.tszCodecName, _T("H264"));

    st_SDPMediaVideo.st_FmtpVideo.nFrameRate = 25;
    st_SDPMediaVideo.st_FmtpVideo.nPacketMode = 1;
    st_SDPMediaVideo.st_FmtpVideo.tszLeaveId[0] = 0x4D;
    st_SDPMediaVideo.st_FmtpVideo.tszLeaveId[1] = 0x40;
    st_SDPMediaVideo.st_FmtpVideo.tszLeaveId[2] = 0x33;
    st_SDPMediaVideo.st_FmtpVideo.nFrameXSize = 1080;
    st_SDPMediaVideo.st_FmtpVideo.nFrameYSize = 720;

    _tcscpy(st_SDPMediaVideo.st_FmtpVideo.tszPPSBase, _T("aO48gA"));
    _tcscpy(st_SDPMediaVideo.st_FmtpVideo.tszSPSBase, _T("Z01AM5p0FCNCAAEEugA9CQEeMGVA"));

    RfcComponents_SDPPacket_AttrVideo(xhToken, &st_SDPMediaVideo);
    //配置音频属性
    RfcComponents_SDPPacket_AddMedia(xhToken, _T("audio"), 97);

    RFCCOMPONENTS_SDPPROTOCOL_MEDIAAUDIO st_SDPMediaAudio;
    memset(&st_SDPMediaAudio, '\0', sizeof(RFCCOMPONENTS_SDPPROTOCOL_MEDIAAUDIO));
    st_SDPMediaAudio.nTrackID = 2;
    st_SDPMediaAudio.st_RTPMap.nChannel = 2;
    st_SDPMediaAudio.st_RTPMap.nCodecNumber = 97;
    st_SDPMediaAudio.st_RTPMap.nSampleRate = 80000;
    _tcscpy(st_SDPMediaAudio.st_RTPMap.tszCodecName, _T("mpeg4-generic"));

    st_SDPMediaAudio.st_FmtpAudio.nConfig = 1400;
    st_SDPMediaAudio.st_FmtpAudio.nDeltaLen = 3;
    st_SDPMediaAudio.st_FmtpAudio.nIndexLen = 3;
    st_SDPMediaAudio.st_FmtpAudio.nProfileID = 1;
    st_SDPMediaAudio.st_FmtpAudio.nSizeLen = 13;
    _tcscpy(st_SDPMediaAudio.st_FmtpAudio.tszMode, "AAC-hbr");
    RfcComponents_SDPPacket_AttrAudio(xhToken, &st_SDPMediaAudio);
    //附加信息
    RfcComponents_SDPPacket_OptionalMediaName(xhToken, "medianame");
    RfcComponents_SDPPacket_OptionalContact(xhToken, "486179@qq.com", "13699444444");
    RfcComponents_SDPPacket_OptionalBandwidth(xhToken, 10000);
    RfcComponents_SDPPacket_OptionalRange(xhToken);
    RfcComponents_SDPPacket_OptionalAddAttr(xhToken, "this is a exp");

    RfcComponents_SDPPacket_GetPacket(xhToken, ptszMsgBuffer, pInt_Len);
    RfcComponents_SDPPacket_Destory(xhToken);
}
void CreateSDP265(TCHAR* ptszMsgBuffer, int* pInt_Len)
{
    XNETHANDLE xhToken = 0;
    RfcComponents_SDPPacket_Create(&xhToken);

    RfcComponents_SDPPacket_Owner(xhToken, _T("qyt"), 123456789, _T("192.168.1.101"));
    RfcComponents_SDPPacket_Session(xhToken, _T("video.h265"));
    RfcComponents_SDPPacket_KeepTime(xhToken);
    //配置视频属性
    RfcComponents_SDPPacket_AddMedia(xhToken, _T("video"), 96);
    RFCCOMPONENTS_SDPPROTOCOL_MEDIAVIDEO st_SDPMediaVideo;
    memset(&st_SDPMediaVideo, '\0', sizeof(RFCCOMPONENTS_SDPPROTOCOL_MEDIAVIDEO));

    st_SDPMediaVideo.nTrackID = 1;
    st_SDPMediaVideo.st_RTPMap.nCodecNumber = 96;
    st_SDPMediaVideo.st_RTPMap.nSampleRate = 90000;
    _tcscpy(st_SDPMediaVideo.st_RTPMap.tszCodecName, _T("H265"));

    st_SDPMediaVideo.st_FmtpVideo.nFrameXSize = 1920;
    st_SDPMediaVideo.st_FmtpVideo.nFrameYSize = 1080;
    st_SDPMediaVideo.st_FmtpVideo.nFrameRate = 24;

    st_SDPMediaVideo.st_FmtpVideo.nProSpace = 0;
    st_SDPMediaVideo.st_FmtpVideo.nProID = 1;
    st_SDPMediaVideo.st_FmtpVideo.nFlags = 0;
    st_SDPMediaVideo.st_FmtpVideo.nLevelID = 0;

    _tcscpy(st_SDPMediaVideo.st_FmtpVideo.tszICStr, _T("000000000000"));
    _tcscpy(st_SDPMediaVideo.st_FmtpVideo.tszLeaveId, _T("QAEMAf//AWAAAAMAAAMAAAMAAAMAAJSQJA=="));
    _tcscpy(st_SDPMediaVideo.st_FmtpVideo.tszPPSBase, _T("RAHBpXwIkA=="));
    _tcscpy(st_SDPMediaVideo.st_FmtpVideo.tszSPSBase, _T("QgEBAWAAAAMAAAMAAAMAAAMAAKAB4CACHH+WUmSRthpYiqkxMMvs+vN+WfXhRGJy7ZA="));

    RfcComponents_SDPPacket_AttrVideo(xhToken, &st_SDPMediaVideo);
    //附加信息
    RfcComponents_SDPPacket_OptionalMediaName(xhToken, "medianame");
    RfcComponents_SDPPacket_OptionalContact(xhToken, "486179@qq.com", "13699444444");
    RfcComponents_SDPPacket_OptionalBandwidth(xhToken, 10000);
    RfcComponents_SDPPacket_OptionalRange(xhToken);
    RfcComponents_SDPPacket_OptionalAddAttr(xhToken, "this is a exp");

    RfcComponents_SDPPacket_GetPacket(xhToken, ptszMsgBuffer, pInt_Len);
    RfcComponents_SDPPacket_Destory(xhToken);
}
void ParseSDP(LPCTSTR lpszMsgBuffer, int nLen)
{
    XNETHANDLE xhToken = 0;
    RfcComponents_SDPParse_Create(&xhToken, lpszMsgBuffer, nLen);

    int nVersion = 0;
    RfcComponents_SDPParse_GetVersion(xhToken, &nVersion);

    __int64 nSessionID = 0;
    __int64 nSessionVer = 0;
    int nIPVer = 0;
    TCHAR tszUserName[64];
    TCHAR tszIPVer[64];

    memset(tszUserName, '\0', sizeof(tszUserName));
    memset(tszIPVer, '\0', sizeof(tszIPVer));

    RfcComponents_SDPParse_GetOwner(xhToken, tszUserName, &nSessionID, &nSessionVer, &nIPVer, tszIPVer);

    nIPVer = 0;
    int nTTL = 0;
    int nCount = 0;
    TCHAR tszIPAddr[64];
    memset(tszIPAddr, '\0', sizeof(tszIPAddr));

    RfcComponents_SDPParse_GetConnect(xhToken, &nIPVer, tszIPAddr, &nTTL, &nCount);

    BOOL bVideo = FALSE;
    TCHAR tszSessionName[64];
    TCHAR tszSessionValue[64];
    memset(tszSessionValue, '\0', sizeof(tszSessionValue));
    memset(tszSessionName, '\0', sizeof(tszSessionName));
    RfcComponents_SDPParse_GetSession(xhToken, tszSessionValue, tszSessionName, &bVideo);

    __int64 nTimeStart = 0;
    __int64 nTimeEnd = 0;
    RfcComponents_SDPParse_GetTime(xhToken, &nTimeStart, &nTimeEnd);

    list<RFCCOMPONENTS_SDPPROTOCOL_AVVIDEO> stl_ListVideo;
    list<RFCCOMPONENTS_SDPPROTOCOL_AVAUDIO> stl_ListAudio;
    RfcComponents_SDPParse_GetMediaVideo(xhToken, &stl_ListVideo);
    RfcComponents_SDPParse_GetMediaAudio(xhToken, &stl_ListAudio);

    TCHAR tszMediaName[64];
    memset(tszMediaName, '\0', sizeof(tszMediaName));
    RfcComponents_SDPParse_OptionalMediaName(xhToken, tszMediaName);

    TCHAR tszEmailAddr[64];
    TCHAR tszPhoneNumber[64];
    memset(tszEmailAddr, '\0', sizeof(tszEmailAddr));
    memset(tszPhoneNumber, '\0', sizeof(tszPhoneNumber));
    RfcComponents_SDPParse_OptionalContact(xhToken, tszEmailAddr, tszPhoneNumber);

    int nBValue = 0;
    int nBType = 0;
    RfcComponents_SDPParse_OptionalBandwidth(xhToken, &nBValue, &nBType);

    list<RFCCOMPONENTS_SDPPROTOCOL_ATTR> stl_ListAttr;
    RfcComponents_SDPParse_OptionalAttr(xhToken, &stl_ListAttr);

    RfcComponents_SDPParse_Destory(xhToken);
}

int main()
{
    int nSDPLen = 0;
    TCHAR tszMsgBuffer[2048];
    memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));

    CreateSDP265(tszMsgBuffer, &nSDPLen);
    printf("%s\n", tszMsgBuffer);

    ParseSDP(tszMsgBuffer, nSDPLen);

    return 0;
}
